package homework;

import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86185
 * Date: 2023-07-25
 * Time: 9:41
 */
class Add {
    long oddSum = 0;
    long evenSum = 0;

    public void addEvenSum(int num) {
        evenSum += num;
    }
    public void addOddSum(int num) {
        oddSum += num;
    }
    public void result() {
        System.out.println("偶数和:" + evenSum + " 奇数和: " + oddSum);
        System.out.println("总和:" + (evenSum + oddSum));
    }
}
public class H230724 {

    public static void main(String[] args) throws InterruptedException {
        long start = System.currentTimeMillis();
        int[] num = new int[1000_0000];
        Random random = new Random();
        for (int i = 0; i < 10000000; i++) {
            num[i] = random.nextInt(100) + 1;
        }
        Add add = new Add();
        Thread thread = new Thread(() -> {
            for (int i = 0; i < 10000000; i += 2) {
                add.addEvenSum(num[i]);
            }
        });
        Thread thread1 = new Thread(() -> {
            for (int i = 1; i < 10000000; i+=2) {
                add.addOddSum(num[i]);
            }
        });
        thread.start();
        thread1.start();
        thread.join();
        thread1.join();
        long end = System.currentTimeMillis();
        add.result();
        System.out.println("时间为: " + (end - start));
    }
}
